package co.com.smart.eagle.client.view.util;


import co.com.smart.eagle.client.util.recursos.Resources;
import co.com.smart.eagle.shared.util.SmartConstantManager;

import com.google.gwt.core.client.Scheduler;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.UIObject;


public class SmartLoading {

	private static SmartLoading instance = null;
	
	private static int contador = 0;
	
	private PopupPanel panel = null;
	private SimplePanel content;
	private Image imagen;
			
	private SmartLoading()
	{
		imagen = new Image(Resources.INSTANCE.cargando());		
		content = new SimplePanel();	
		content.add(imagen);
		content.getElement().getStyle().setZIndex(SmartConstantManager.Z_INDEX_CARGANDO + 1);
		content.getElement().setPropertyString("border", "none");
		initPanel();	
	}
	/**
	 * JavaScript interpreters are single-threaded, so while GWT silently accepts the synchronized keyword,
	 * it has no real effect. Synchronization-related library methods are not available, 
	 * including Object.wait(), Object.notify(), and Object.notifyAll().
	 */
	public synchronized void hide()
	{
		contador --;
		if (contador < 0)
		{
			System.err.println("El Componente de SmartLoading() tiene llamados no sincronizados");
			contador = 0;
		}
		if (contador == 0)
		{
			panel.hide();			
		}
		
	
	}
	
	public static SmartLoading getInstance ()
	{
		if (instance == null)
		{
			instance = new SmartLoading();			
		}
		return instance;
	}
	
	private void initPanel()
	{
		panel = new PopupPanel(false);
		panel.getElement().setPropertyString("contenteditable", "true");
		panel.getElement().setPropertyString("align", "center");
		panel.getElement().setTabIndex(1);	
		panel.setStyleName("gwt-PopupPanel",false);
		panel.setAnimationEnabled(true);
		panel.setGlassStyleName("gwt-PopupPanelGlass");//ClauGlass
		panel.getElement().getStyle().setZIndex(SmartConstantManager.Z_INDEX_CARGANDO);
		panel.setModal(true);
		panel.add(content);										
		panel.setGlassEnabled(true);
		panel.getElement().setPropertyString("border", "none");
	}
	/**
	 * JavaScript interpreters are single-threaded, so while GWT silently accepts the synchronized keyword,
	 * it has no real effect. Synchronization-related library methods are not available, 
	 * including Object.wait(), Object.notify(), and Object.notifyAll().
	 */
	public synchronized void show(UIObject modal)
	{
		mostrar(modal);
	}
	
	/**
	 * JavaScript interpreters are single-threaded, so while GWT silently accepts the synchronized keyword,
	 * it has no real effect. Synchronization-related library methods are not available, 
	 * including Object.wait(), Object.notify(), and Object.notifyAll().
	 */
	public synchronized void show()
	{
		mostrar(null);
	}
	
	private void mostrar(UIObject modal)
	{
		contador ++;
		if (contador == 1)
		{			
			panel.setVisible(true);
			panel.center();
			if (modal != null)
			{
				panel.showRelativeTo(modal);
			}
			else
			{			
				panel.show();
				Scheduler.get().scheduleDeferred(new Command() {
			        public void execute() {
			        	panel.getElement().focus();
			        }
			    });
			}
		}		
	}
	
}
